package cn.iocoder.yudao.module.system.service.erp.warehousein;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehousein.vo.WarehouseInPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehousein.vo.WarehouseInSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehousein.WarehouseInDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.warehousein.WarehouseInMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WAREHOUSE_IN_NOT_EXISTS;

/**
 * 入库单 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseInServiceImpl implements WarehouseInService {

    @Resource
    private WarehouseInMapper warehouseInMapper;

    @Override
    public Long createWarehouseIn(WarehouseInSaveReqVO createReqVO) {
        // 插入
        WarehouseInDO warehouseIn = BeanUtils.toBean(createReqVO, WarehouseInDO.class);
        warehouseInMapper.insert(warehouseIn);
        // 返回
        return warehouseIn.getId();
    }

    @Override
    public void updateWarehouseIn(WarehouseInSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseInExists(updateReqVO.getId());
        // 更新
        WarehouseInDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseInDO.class);
        warehouseInMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseIn(Long id) {
        // 校验存在
        validateWarehouseInExists(id);
        // 删除
        warehouseInMapper.deleteById(id);
    }

    private void validateWarehouseInExists(Long id) {
        if (warehouseInMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_IN_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseInDO getWarehouseIn(Long id) {
        return warehouseInMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseInDO> getWarehouseInPage(WarehouseInPageReqVO pageReqVO) {
        return warehouseInMapper.selectPage(pageReqVO);
    }

}